package com.jh.user.pojo.freeQuick;

import cn.jh.common.pay.interfaces.RetCode;
import cn.jh.common.pay.jdpush.CalFeeStatus;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import static org.springframework.util.StringUtils.hasText;

/**
 * @author Yichuan
 * @version 1.0
 */
@JsonPropertyOrder(value = {
        "ret_code", "ret_msg", "out_serial_no", "amount", "status", "fee", "is_cal_fee", "sign"
})
public class OutQueryConsume extends OutEntity {

    @NotNull
    @Contract(" -> new")
    public static OutQueryConsume success() {
        return new OutQueryConsume(RetCode.SUCCESS);
    }

    @NotNull
    @Contract("_ -> new")
    public static OutQueryConsume of(RetCode retCode) {
        return new OutQueryConsume(retCode);
    }

    @NotNull
    @Contract("_, _ -> new")
    public static OutQueryConsume of(String retCode, String retMsg) {
        return new OutQueryConsume(retCode, retMsg);
    }

    private String outSerialNo;
    private BigDecimal amount;
    private String status;
    private BigDecimal fee;
    private Boolean isCalFee;

    public OutQueryConsume(RetCode retCode) {
        super(retCode);
    }

    public OutQueryConsume(String retCode, String retMsg) {
        super(retCode, retMsg);
    }

    @JsonProperty("out_serial_no")
    public String getOutSerialNo() {
        return outSerialNo;
    }

    public void setOutSerialNo(String outSerialNo) {
        this.outSerialNo = outSerialNo;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public BigDecimal getFee() {
        return fee;
    }

    public void setFee(BigDecimal fee) {
        this.fee = fee;
    }

    @JsonProperty("is_cal_fee")
    public Boolean isCalFee() {
        return isCalFee;
    }

    public void setCalFee(Boolean calFee) {
        isCalFee = calFee;
    }

    @Override
    protected void applySign(List<String> parts) {
        parts.add(outSerialNo);
        if (amount != null) {
            parts.add(amount.toPlainString());
        }
        if (status != null) {
            parts.add(status);
        }
        if (fee != null) {
            parts.add(fee.toPlainString());
        }
        if (isCalFee != null) {
            parts.add(isCalFee ? "1" : "0");
        }
    }

    public OutQueryConsume with(String outSerialNo, String amount, String status, String fee, String isCalFee) {
        setOutSerialNo(outSerialNo);
        if (hasText(amount)) {
            setAmount(new BigDecimal(amount).setScale(2, RoundingMode.HALF_UP));
        }
        if (hasText(status)) {
            setStatus(status);
        }
        if (hasText(fee)) {
            setFee(new BigDecimal(fee).setScale(2, RoundingMode.HALF_UP));
        }
        if (hasText(isCalFee)) {
            setCalFee(isCalFee.equals("1"));
        }
        return this;
    }

    public OutQueryConsume with(String outSerialNo, BigDecimal amount, String status, BigDecimal fee, CalFeeStatus feeStatus) {
        setOutSerialNo(outSerialNo);
        if (amount != null) {
            setAmount(amount.setScale(2, RoundingMode.HALF_UP));
        }
        if (hasText(status)) {
            setStatus(status);
        }
        if (fee != null) {
            setFee(fee.setScale(2, RoundingMode.HALF_UP));
        }
        if (feeStatus != null) {
            setCalFee(feeStatus == CalFeeStatus.YES);
        }
        return this;
    }
}
